<?php

declare (strict_types = 1);

namespace auth;

trait GuardHelper
{
    /**
     * @var \auth\contract\Authenticatable
     */
    protected $user;

    /**
     * @var \auth\contract\UserProvider
     */
    protected $provider;

    /**
     * 确定当前用户是否通过认证
     *
     * @return bool
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function check(): bool
    {
        return !is_null($this->user());
    }

    /**
     * 确定当前用户的身份是否为游客
     *
     * @return bool
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function guest(): bool
    {
        return !$this->check();
    }

    /**
     * 获取认证用户的 ID
     *
     * @return mixed|void
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function id()
    {
        if ($this->user()) {
            return $this->user()->getAuthIdentifier();
        }
    }
}
